package com.hoshiibuy.platform.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hoshiibuy.common.aop.Logable;
import com.hoshiibuy.common.exception.CommonError;
import com.hoshiibuy.common.exception.ServiceException;
import com.hoshiibuy.platform.entity.OrganizationType;
import com.hoshiibuy.platform.feign.IdGeneratorService;
import com.hoshiibuy.platform.mapper.OrganizationTypeMapper;
import com.hoshiibuy.platform.service.OrganizationTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 机构类型 服务实现类
 * </p>
 *
 * @author yl
 * @since 2019-04-22
 */
@Service
public class OrganizationTypeServiceImpl extends ServiceImpl<OrganizationTypeMapper, OrganizationType> implements OrganizationTypeService {

    @Autowired
    private IdGeneratorService idGeneratorService;


    /**
     * @param id
     * @return com.hoshiibuy.platform.entity.OrganizationType
     * @description: 根据id查询平台机构类型信息
     */
    @Cacheable(cacheNames = "OrganizationType", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#id")
    @Logable(authName = "平台管理-平台机构类型信息管理-查询平台机构类型信息")
    public OrganizationType getById(Long id) {

        return baseMapper.selectById(id);
    }


    /**
     * @param entity
     * @return boolean
     * @description: 新增平台机构类型
     */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "OrganizationType", allEntries = true)
    @Logable(authName = "平台管理-平台机构类型信息管理-新增平台机构类型信息")
    public boolean save(OrganizationType entity) {

        String id = idGeneratorService.queryGeneratorId();

        if (id == null || id == "") {
            throw new ServiceException(CommonError.error("新增失败"));
        }

        entity.setId(Long.parseLong(id));
        entity.setCreatedtime(new Date());

        int row = baseMapper.insert(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("新增失败"));
        }
        return true;
    }

    /**
     * @param entity
     * @return boolean
     * @description: 修改平台机构类型信息
     */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "OrganizationType", allEntries = true)
    @Logable(authName = "平台管理-平台机构类型信息管理-修改平台机构类型信息")
    public boolean updateById(OrganizationType entity) {

        entity.setModifiedtime(new Date());
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("修改失败"));
        }
        return true;
    }

    /**
     * @param queryWrapper
     * @return IPage
     * @description: 分页查询平台机构类型信息
     */
    @Logable(authName = "平台管理-平台机构类型信息管理-分页查询平台机构类型信息")
    @Cacheable(cacheNames = "OrganizationType", key = "T(com.hoshiibuy.common.utils.DigestUtils).md5('Wrapper(SqlSelect=' + #queryWrapper.getSqlSelect() + ', SqlSegment=' + #queryWrapper.getSqlSegment() + ')')")
    public List<OrganizationType> list(Wrapper<OrganizationType> queryWrapper) {
        return baseMapper.selectList(queryWrapper);
    }
}
